// friendFeedBadge by Robert Nyman, http://www.robertnyman.com/, http://code.google.com/p/friendfeedbadge/
var friendFeedBadge = function () {
	// User settings
	var settings = {
		userName : "robertnyman", // Your Friendfeed user name
		itemsToShow : 5, // Number of Friendfreed items to retrieve
		itemToAddBadgeTo : "friendfeed-badge", // Id of HTML element to put badge code into
		imageSize : 100, // Suggested values: 75, 100, 250, 400 or 500
		showServiceIcons : true, // Apply icons for each service from Friendfeed. Each list item get a service class name, for custom CSS styling
		timeToWait : 2000 // Milliseconds, 1000 = 1 second
	};
	
	// Script element creation
	var head = document.getElementsByTagName("head")[0];
	var badgeContainer = document.getElementById(settings.itemToAddBadgeTo);
	if (head && badgeContainer) {
		var badgeJSON = document.createElement("script");
		badgeJSON.type = "text/javascript";
		badgeJSON.src = "http://friendfeed.com/api/feed/user/" + settings.userName + "?callback=friendFeedBadge.listItems&num=" + settings.itemsToShow;
		head.appendChild(badgeJSON);

		var wait = setTimeout(function () {
			friendFeedBadge.listItems = function () {};
			badgeJSON.parentNode.removeChild(badgeJSON);
			badgeJSON = null;
		}, settings.timeToWait);
	}
	
	return {
		// Badge functionality
		listItems : function (json) {
			var posts = json.entries;
			var list = document.createElement("ul"), 
				post, 
				listItem, 
				text, 
				link, 
				img, 
				postLink;
			list.className = "friendfeed";
			for (var i=0, il=posts.length; i<il; i=i+1) {
				post = posts[i];
				listItem = document.createElement("li");
				listItem.className = post.service.id;
				text = post.title;
				if (settings.showServiceIcons) {
					listItem.style.background = "url(" + post.service.iconUrl + ") no-repeat";
				}
				if (/flickr|smugmug|blog/i.test(post.service.id)) {
					link = document.createElement("a");
					link.href = post.link;
					if (post.media.length > 0) {
						img = document.createElement("img");
						// To avoid a creeping page
						img.width = settings.imageSize;
						img.src = post.media[0].thumbnails[0].url;
						link.appendChild(img);
					}
					link.innerHTML += text;
					listItem.appendChild(link);
				}
				else {
					listItem.innerHTML += "\"" + text + "\"";
				}

				var publishedDate = post.published.match(/(\d{4})\-(\d{2})\-(\d{2})T(\d{2}):(\d{2})/);
				var now = new Date();
				var publication = {
					date : publishedDate[3],
					month : publishedDate[2] - 1,
					year : publishedDate[1],
					hours : publishedDate[4],
					minutes : publishedDate[5]
				};
				var published = new Date();
				published.setDate(publication.date);
				published.setMonth(publication.month);
				published.setYear(publication.year);
				published.setHours(publication.hours);
				published.setMinutes(publication.minutes);
				
				var timeAgo = now - published;
				timeAgo = (timeAgo / 1000) / 60;
				var timeAgoText = timeAgo + " minutes ago";
				if (timeAgo > 60) {
					timeAgo = parseInt(timeAgo / 60, 10);
					timeAgoText = timeAgo + " hours ago";
				}
				if (timeAgo > 23) {
					var weekDays = [
						"Monday",
						"Tuesday",
						"Wednesday",
						"Thursday",
						"Friday",
						"Saturday",
						"Sunday"
					];
					timeAgoText = weekDays[published.getDay()] + " at " + publication.hours + ":" + publication.minutes;
				}
				
				// Post published info
				postLink = document.createElement("a");
				postLink.className = "friendfeed-post-date";
				postLink.href = post.link;
				postLink.innerHTML = timeAgoText;
				listItem.appendChild(postLink);

				// Apply list item to list
				list.appendChild(listItem);
			}

			// Apply list to container element
			badgeContainer.innerHTML = "";
			badgeContainer.appendChild(list);
		}
	};	
}();